﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Text;

namespace Kang.ExtendMethod
{
    public static class EnumExtendMethod
    {
        /// <summary>
        /// 获取枚举类型的描述(System.ComponentModel.DescriptionAttribute)
        /// </summary>
        /// <returns></returns>
        public static string GetDescription(this Enum _enum)
        {
            Type type = _enum.GetType();
            MemberInfo[] memInfo = type.GetMember(_enum.ToString());
            if (null != memInfo && memInfo.Length > 0)
            {
                object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
                if (null != attrs && attrs.Length > 0)
                    return ((DescriptionAttribute)attrs[0]).Description;
            }
            return _enum.ToString();
        }

        /// <summary>
        /// 获取枚举类型的名称
        /// </summary>
        /// <returns></returns>
        public static string GetName(this Enum _enum)
        {
            if (_enum == null)
            {
                return "";
            }
            return Enum.GetName(_enum.GetType(), _enum);
        }
    }
}
